/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.app;

import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppMarketingRuleSaveParam;
import com.jnzc.ocr.serv.shop.api.ShopMarketingRuleApi;
import com.jnzc.ocr.serv.shop.consts.ShopMarketingTypeConsts;
import com.jnzc.ocr.serv.shop.entity.ShopMarketingRule;
import com.jnzc.ocr.serv.shop.filter.ShopMarketingRuleFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

/**
 * @author zhangcm
 * @version 1.0, 2017/9/13
 */
@Api(tags = {"app - 营销规则接口"})
@RestController
@RequestMapping("/app/marketingRules")
public class AppMarketingRuleController extends BaseController {

    @Autowired
    private ShopMarketingRuleApi shopMarketingRuleApi;

    @ApiOperation(value = "分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "marketingType", value = "营销类型", required = true, paramType = "query")
    })
    @GetMapping("")
    public PageResult<ShopMarketingRule> search(Integer marketingType, Page page){
        ValidateUtils.notNull(marketingType, "营销类型");
        UserVo userVo = super.get();
        ShopMarketingRuleFilter marketingRuleFilter = new ShopMarketingRuleFilter();
        marketingRuleFilter.setShopId(userVo.getShopId());
        marketingRuleFilter.setMarketingType(marketingType);
        return shopMarketingRuleApi.search(marketingRuleFilter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "营销规则ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public ShopMarketingRule info(@PathVariable Long id){
        UserVo userVo = super.get();
        return shopMarketingRuleApi.find(id, userVo.getShopId());
    }

    @ApiOperation(value = "保存")
    @PostMapping("")
    public void save(@RequestBody AppMarketingRuleSaveParam param){
        ValidateUtils.notNull(param.getMarketingType(), "营销类型");
        UserVo userVo = super.get();
        ShopMarketingRule obj;
        if(null == param.getId()){
            obj = new ShopMarketingRule();
            obj.setShopId(userVo.getShopId());
        } else {
            obj = shopMarketingRuleApi.find(param.getId(), userVo.getShopId());
        }
        obj.setMarketingType(param.getMarketingType());
        obj.setMarketingTypeName(null);
        obj.setAmountLine(param.getAmountLine());
        obj.setPoint(null);
        obj.setAmount(null);
        obj.setDiscount(null);
        if(ShopMarketingTypeConsts.POINT == obj.getMarketingType()){
            obj.setPoint(param.getPoint());
        } else if(ShopMarketingTypeConsts.AMOUNT == obj.getMarketingType()){
            obj.setAmount(param.getAmount());
        } else if(ShopMarketingTypeConsts.DISCOUNT == obj.getMarketingType()){
            obj.setDiscount(param.getDiscount());
        }
        shopMarketingRuleApi.save(obj);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "营销规则ID", required = true, paramType = "path")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id){
        UserVo userVo = super.get();
        shopMarketingRuleApi.delete(id, userVo.getShopId());
    }
}
